package com.orange.score.android.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import com.orange.score.activity.entry.MainActivity;
import com.orange.score.app.ScoreApplication;
import com.orange.score.model.config.ConfigManager;
import com.orange.score.model.match.FollowMatchManager;
import com.orange.score.model.match.Match;
import com.orange.score.model.match.MatchManager;
import com.orange.score.model.match.ScoreUpdate;
import com.orange.score.service.LiveUpdateChangeCallBack;
import com.orange.score.service.MatchService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ScoreUpdateService extends Service
  implements LiveUpdateChangeCallBack
{
  public static final String SCORE_UPDATE_BROADCAST = "SCORE_UPDATE_BROADCAST";
  private static final int SCORE_UPDATE_CHANGE = 1;
  private static final String SCORE_UPDATE_TYPE = "SCORE_UPDATE_TYPE";
  private static final String TAG = ScoreUpdateService.class.getName();
  static List<ScoreUpdate> scoreUpdateList = new ArrayList();
  static HashMap<String, ScoreUpdate> scoreUpdateMap = new HashMap();
  private int messageNotificationID = 1000;

  private void addNotificaction(ScoreUpdate paramScoreUpdate)
  {
    NotificationManager localNotificationManager = (NotificationManager)getSystemService("notification");
    Notification localNotification = new Notification(2130837635, "球探体育比分新消息", System.currentTimeMillis());
    localNotification.flags = 16;
    ConfigManager localConfigManager = ConfigManager.getInstance();
    if (localConfigManager.isVibrate())
    {
      localNotification.defaults = (0x2 | localNotification.defaults);
      localNotification.defaults = (0x4 | localNotification.defaults);
    }
    if (localConfigManager.isSound())
    {
      new MediaPlayer();
      MediaPlayer localMediaPlayer = MediaPlayer.create(this, 2131099648);
      localMediaPlayer.setAudioStreamType(3);
      localMediaPlayer.setLooping(false);
      localMediaPlayer.start();
    }
    Intent localIntent = new Intent(this, MainActivity.class);
    localIntent.setFlags(335544320);
    PendingIntent localPendingIntent = PendingIntent.getActivity(this, 0, localIntent, 134217728);
    RemoteViews localRemoteViews = new RemoteViews(getPackageName(), 2130903080);
    localRemoteViews.setTextViewText(2131558498, paramScoreUpdate.leagueName);
    localRemoteViews.setTextViewText(2131558502, paramScoreUpdate.homeTeamName);
    localRemoteViews.setTextViewText(2131558503, paramScoreUpdate.awayTeamName);
    localRemoteViews.setTextViewText(2131558500, paramScoreUpdate.minutes);
    localRemoteViews.setTextViewText(2131558505, paramScoreUpdate.homeTeamScore);
    localRemoteViews.setTextViewText(2131558506, paramScoreUpdate.awayTeamScore);
    localNotification.contentView = localRemoteViews;
    localNotification.contentIntent = localPendingIntent;
    localNotificationManager.notify(this.messageNotificationID, localNotification);
    this.messageNotificationID = (1 + this.messageNotificationID);
  }

  private void addScoreUpdate(String paramString, ScoreUpdate paramScoreUpdate)
  {
    scoreUpdateList.add(0, paramScoreUpdate);
    scoreUpdateMap.put(paramString, paramScoreUpdate);
  }

  private void broadcastScoreUpdate()
  {
    Log.d(TAG, "broadcastScoreUpdate");
    Intent localIntent = new Intent("SCORE_UPDATE_BROADCAST");
    localIntent.putExtra("SCORE_UPDATE_TYPE", 1);
    sendBroadcast(localIntent);
  }

  public static void clearData()
  {
    scoreUpdateList.clear();
    scoreUpdateMap.clear();
  }

  private void fireScoreUpdateNotification(ScoreUpdate paramScoreUpdate, String paramString)
  {
    if (!(ConfigManager.getInstance().isScorePush()));
    while (true)
    {
      do
        while (true)
        {
          return;
          if (ScoreApplication.isBackground())
            break;
          Log.i(TAG, "Application is not in background.");
        }
      while ((paramScoreUpdate.updateType != 0) || (!(FollowMatchService.getFollowMatchManager().isMatchFollowed(paramString))));
      addNotificaction(paramScoreUpdate);
    }
  }

  public static List<ScoreUpdate> getScoreUpdateList()
  {
    return scoreUpdateList;
  }

  public ScoreUpdate createScoreUpdateObject(Match paramMatch, String[] paramArrayOfString)
  {
    Object localObject;
    if ((paramMatch == null) || (paramArrayOfString == null) || (paramArrayOfString.length == 0))
      localObject = null;
    while (true)
    {
      return localObject;
      ScoreUpdate localScoreUpdate1 = new ScoreUpdate();
      int i = 0;
      int j = 0;
      int k = 0;
      int l = 0;
      int i1 = 0;
      int i2 = 0;
      String str1 = "0";
      String str2 = "0";
      String str3 = "0";
      String str4 = "0";
      String str5 = "0";
      String str6 = "0";
      if (scoreUpdateMap.containsKey(paramMatch.matchId))
      {
        ScoreUpdate localScoreUpdate2 = (ScoreUpdate)scoreUpdateMap.get(paramMatch.matchId);
        if (localScoreUpdate2 != null)
        {
          str1 = localScoreUpdate2.homeTeamScore;
          str2 = localScoreUpdate2.awayTeamScore;
          str3 = localScoreUpdate2.homeTeamRed;
          str4 = localScoreUpdate2.awayTeamRed;
          str5 = localScoreUpdate2.homeTeamYellow;
          str6 = localScoreUpdate2.awayTeamYellow;
        }
        if (isDataChange(paramArrayOfString[4], str1))
          i = 1;
        if (isDataChange(paramArrayOfString[5], str2))
          l = 1;
        if (isDataChange(paramArrayOfString[8], str3))
          j = 1;
        if (isDataChange(paramArrayOfString[9], str4))
          i1 = 1;
        if (isDataChange(paramArrayOfString[10], str5))
          k = 1;
        if (isDataChange(paramArrayOfString[11], str6))
          i2 = 1;
        if ((i == 0) && (l == 0))
          break label518;
        localScoreUpdate1.updateType = 0;
        if ((i == 0) || (l == 0))
          break label490;
        localScoreUpdate1.homeAwayFlag = 0;
      }
      while (true)
      {
        do
          while (true)
          {
            while (true)
            {
              while (true)
              {
                do
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        do
                          while (true)
                          {
                            while (true)
                            {
                              while (true)
                              {
                                localScoreUpdate1.score = paramArrayOfString[4].concat(":").concat(paramArrayOfString[5]);
                                localScoreUpdate1.redCardScore = paramArrayOfString[8].concat(":").concat(paramArrayOfString[9]);
                                localScoreUpdate1.yellowCardScore = paramArrayOfString[10].concat(":").concat(paramArrayOfString[11]);
                                localScoreUpdate1.homeTeamScore = paramArrayOfString[4];
                                localScoreUpdate1.awayTeamScore = paramArrayOfString[5];
                                localScoreUpdate1.homeTeamRed = paramArrayOfString[8];
                                localScoreUpdate1.awayTeamRed = paramArrayOfString[9];
                                localScoreUpdate1.homeTeamYellow = paramArrayOfString[10];
                                localScoreUpdate1.awayTeamYellow = paramArrayOfString[11];
                                localScoreUpdate1.scoreUpdateData = paramArrayOfString;
                                localScoreUpdate1.minutes = paramMatch.matchMinuteString();
                                localScoreUpdate1.homeTeamName = paramMatch.homeTeamName;
                                localScoreUpdate1.awayTeamName = paramMatch.awayTeamName;
                                localScoreUpdate1.date = paramMatch.getDateString();
                                localScoreUpdate1.leagueName = paramMatch.getLeagueName();
                                localScoreUpdate1.leagueColor = paramMatch.getLeagueColor();
                                localObject = localScoreUpdate1;
                              }
                              str1 = paramMatch.homeTeamScore;
                              str2 = paramMatch.awayTeamScore;
                              str3 = paramMatch.homeTeamRed;
                              str4 = paramMatch.awayTeamRed;
                              str5 = paramMatch.homeTeamYellow;
                              str6 = paramMatch.awayTeamYellow;
                            }
                            label490: if (i == 0)
                              break;
                            localScoreUpdate1.homeAwayFlag = 1;
                          }
                        while (l == 0);
                        localScoreUpdate1.homeAwayFlag = 2;
                      }
                      label518: if ((j == 0) && (i1 == 0))
                        break label581;
                      localScoreUpdate1.updateType = 1;
                      if ((j == 0) || (i1 == 0))
                        break;
                      localScoreUpdate1.homeAwayFlag = 0;
                    }
                    if (j == 0)
                      break;
                    localScoreUpdate1.homeAwayFlag = 1;
                  }
                while (i1 == 0);
                localScoreUpdate1.homeAwayFlag = 2;
              }
              label581: if ((k == 0) && (i2 == 0))
                break label644;
              localScoreUpdate1.updateType = 2;
              if ((k == 0) || (i2 == 0))
                break;
              localScoreUpdate1.homeAwayFlag = 0;
            }
            if (k == 0)
              break;
            localScoreUpdate1.homeAwayFlag = 1;
          }
        while (i2 == 0);
        localScoreUpdate1.homeAwayFlag = 2;
      }
      label644: localObject = null;
    }
  }

  public boolean isDataChange(String paramString1, String paramString2)
  {
    int i;
    if ((paramString2 == null) || (paramString1 == null))
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        int j = 0;
        int k = 0;
        if (paramString2.length() > 0)
          j = Integer.parseInt(paramString2);
        if (paramString1.length() > 0)
          k = Integer.parseInt(paramString1);
        if (k <= j)
          break;
        i = 1;
      }
      i = 0;
    }
  }

  public void notifyScoreLiveUpdate(List<String[]> paramList)
  {
    MatchManager localMatchManager;
    int i;
    int k;
    if ((paramList == null) || (paramList.size() == 0));
    while (true)
    {
      do
      {
        return;
        localMatchManager = ((ScoreApplication)getApplication()).getRealtimeMatchService().getMatchManager();
        i = 0;
        int j = paramList.size();
        k = 0;
        if (k < j)
          break label59;
      }
      while (i == 0);
      broadcastScoreUpdate();
    }
    label59: String[] arrayOfString = (String[])paramList.get(k);
    if ((arrayOfString == null) || (arrayOfString.length == 0))
      Log.d(TAG, "notifyScoreLiveUpdate but data null or empty");
    while (true)
    {
      Match localMatch;
      ScoreUpdate localScoreUpdate;
      do
      {
        while (true)
        {
          while (true)
            ++k;
          localMatch = localMatchManager.findMatchById(arrayOfString[0]);
          if (localMatch != null)
            break;
          Log.d(TAG, "notifyScoreLiveUpdate but match id not found, match id = " + arrayOfString[0]);
        }
        localScoreUpdate = createScoreUpdateObject(localMatch, arrayOfString);
      }
      while (localScoreUpdate == null);
      Log.d(TAG, "score update has change, matchId=" + localMatch.getMatchId() + ", type = " + localScoreUpdate.updateType + ", home/away flag = " + localScoreUpdate.homeAwayFlag);
      i = 1;
      addScoreUpdate(localMatch.matchId, localScoreUpdate);
      fireScoreUpdateNotification(localScoreUpdate, localMatch.matchId);
      localMatch.latestScoreUpdateTime = System.currentTimeMillis();
    }
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onCreate()
  {
    this.messageNotificationID = (int)(System.currentTimeMillis() / 1000L);
    super.onCreate();
    Log.d(TAG, toString() + " <onCreate>");
    ((ScoreApplication)getApplication()).getRealtimeMatchService().addScoreLiveUpdateObserverAsFirst(this);
  }

  public void onStart(Intent paramIntent, int paramInt)
  {
    super.onStart(paramIntent, paramInt);
    Log.d(TAG, toString() + " <onStart>");
    broadcastScoreUpdate();
  }
}